"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Query_Dop并行查询
Case Name   : 设置query_dop=4,执行查询
Create At   : 2022/11/18
Owner       : @zou_jialiang050
Description :
    1.建表
    2.向表插入数据
    3.设置query_dop=4
    4.执行查询
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class QUERY(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.userNode = Node('PrimaryDbUser')
        self.database = self.userNode.db_name
        self.com = Common()
        self.table = 't_query_dop_test_0039'

    def test_query_dop(self):
        text = '-----step1:建表;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table};" \
            f"create table {self.table} (c_int int);"
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result, '执行失败' +
                      text)

        text = '-----step2:向表插入数据;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'insert into {self.table} values(generate_series(1,100));'
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, text +
                      '执行失败')

        text = '-----step3:设置query_dop=4;expect:成功-----'
        self.logger.info(text)
        self.query_dop = self.com.show_param('query_dop')
        sql_cmd = f'alter database {self.database} set query_dop to 4;'
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, result, text +
                      '执行失败')
        result = self.sh_primy.restart_db_cluster(True)
        self.assertTrue(result, '执行失败' + text)

        text = '-----step4:执行查询;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'select count(*) from {self.table};'
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result)
        self.assertEqual(str(result.splitlines()[-2]).strip(), '100', text +
                         '执行失败')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.logger.info(text)
        clean_cmd = f"drop table {self.table};"
        self.logger.info(clean_cmd)
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        sql_cmd = f'alter database {self.database} set query_dop to ' \
            f'{self.query_dop};'
        self.logger.info(sql_cmd)
        result1 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result1)
        result2 = self.sh_primy.restart_db_cluster(True)
        self.assertTrue(result2, '执行失败' + text)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, result1, text +
                      '执行失败')
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_msg, text +
                      '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
